//
//  CustomeUpdateView.m
//  ContinueEducationProject
//
//  Created by 中税网 on 2020/10/21.
//  Copyright © 2020 TaxChina. All rights reserved.
//

#import "CustomeUpdateView.h"
#import "CEHeader.h"
#import <Masonry/Masonry.h>

@interface CustomeUpdateView()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIImageView * headerImageView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIButton *confirmButton;//确定按钮 立即更新按钮
@property (nonatomic, strong) UIButton *cancleButton;//取消按钮

// 内容的默认高度
@property (nonatomic, assign) CGFloat contentH;


@end


@implementation CustomeUpdateView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.4];
        [self createSubViews];
    }
    return self;
}
- (void)createSubViews
{
    [self addSubview:self.bgView];
    self.contentH = 80 ;
    [self.bgView addSubview:self.headerImageView];
    [self.bgView addSubview:self.contentView];

    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.confirmButton];
    
    [self addSubview:self.cancleButton];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(45);
        make.right.equalTo(self).offset(-45);
        make.height.mas_equalTo(self.contentH + 188+114);
        make.center.equalTo(self);
    }];
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(188);
    }];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.headerImageView.mas_bottom).offset(-11);
    }];
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(24);
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(20);
        make.height.mas_equalTo(self.contentH);
    }];
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(50);
        make.bottom.mas_equalTo(-20);
    }];
    
    [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(self.bgView.mas_bottom).offset(20);
    }];
   
}



#pragma mark - 从父视图上面移除
- (void)removeUpdateView
{
    [UIView animateWithDuration:.5f animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (self) {
            [self removeFromSuperview];
        }
    }];
}


- (void)showUpdateView:(NSString *)updateMsg  isForce:(BOOL)isForce
{
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [UIView animateWithDuration:.5f animations:^{
        self.alpha = 1;
        self.contentLabel.text = updateMsg;
        [self setLineSpaceLabel:self.contentLabel];
        self.contentH = [self getSpaceLabelHeight:updateMsg withFont:[UIFont systemFontOfSize:14] withWidth:kScreenWidth-120]+20;
        self.cancleButton.hidden = isForce ;
    }];
}


#pragma mark - 设置行间距
- (void)setLineSpaceLabel:(UILabel *)label
{
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:label.text];
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    paraStyle.lineSpacing = 9;
    [attr addAttribute:NSParagraphStyleAttributeName value:paraStyle range:NSMakeRange(0, label.text.length)];
    label.attributedText = attr;
}


#pragma mark - lazy loading

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor clearColor];
        
    }
    return _bgView;
}
-(UIImageView *)headerImageView{
    if (!_headerImageView) {
        _headerImageView = [[UIImageView alloc]init];
        _headerImageView.image = [UIImage imageNamed:@"update_headerImg"];
    }
    return _headerImageView;
}
-(UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor  = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 11;
    }
    return _contentView;
}
- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text = @"发现新版本";
        _titleLabel.textColor = k_Color(51, 51, 51, 1.0);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = [UIFont systemFontOfSize:17 weight:UIFontWeightSemibold];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.backgroundColor = [UIColor whiteColor];
    }
    return _titleLabel;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
//        _contentLabel.text = @"我是更新的内容";
        _contentLabel.textColor = k_Color(51, 51, 51, 1.0);
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.backgroundColor = [UIColor whiteColor];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}




// 立即更新按钮  确定按钮
- (UIButton *)confirmButton
{
    if (!_confirmButton) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setImage:[UIImage imageNamed:@"update_update"] forState:UIControlStateNormal];
        _confirmButton.tag = 1002;
        [_confirmButton addTarget:self action:@selector(tapUpdateButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmButton;
}
//取消按钮
- (UIButton *)cancleButton
{
    if (!_cancleButton) {
        _cancleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancleButton setImage:[UIImage imageNamed:@"update_cancel"] forState:UIControlStateNormal];
        _cancleButton.tag = 1001;
        [_cancleButton addTarget:self action:@selector(tapCancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancleButton;
}

- (CGFloat)getSpaceLabelHeight:(NSString*)str withFont:(UIFont*)font withWidth:(CGFloat)width
{
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.alignment = NSTextAlignmentLeft;
    paraStyle.lineSpacing = 9;
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle};
    CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    return size.height;
}

-(void)tapUpdateButtonAction:(id)sender{
    if (self.updateBlock) {
        self.updateBlock(sender);
    }
    [self removeUpdateView];
}
-(void)tapCancelButtonAction:(id)sender{
    [self removeUpdateView];
}
@end
